package com.oa;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;


import java.io.IOException;

/**
 * Created by Lianglh on 2019-11-26 14:39.
 */
public class HttpClientUtil {

    public  static String doGet(String apiURI) {
        // 创建httpClient
        CloseableHttpClient client = HttpClientBuilder.create().build();
        // 创建HttpGet请求
        HttpGet httpGet = new HttpGet(apiURI);
        CloseableHttpResponse response = null;
        HttpEntity entity = null;
        //将实体装成字符串
        String string = null;
        try {
            response = client.execute(httpGet);
            // 获取实体
            entity = response.getEntity();
            string = EntityUtils.toString(entity);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                // 释放资源
                if (client != null) {
                    client.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return string;
    }

}
